package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.mapper.MemberNPMapper;
import com.zzyl.nursing.service.MemberNPService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class MemberNPServiceImpl extends ServiceImpl<MemberNPMapper, NursingProject> implements MemberNPService {
    private final RedisTemplate<Object, Object> redisTemplate;

    public MemberNPServiceImpl(RedisTemplate<Object, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Autowired
    private MemberNPMapper memberNPMapper;


    //抽取出redis的key
    private static final String PROJECT_KEY = "MemberProject:all";

    /**
     * 分页查询护理项目列表
     *
     * @param nursingProject
     * @return
     */
    @Override
    public List<NursingProject> selectProjectPage(NursingProject nursingProject) {
        //从缓存中获取
        List<NursingProject> nursingProjects = (List<NursingProject>) redisTemplate.opsForValue().get(PROJECT_KEY);


        //缓存中有数据，直接返回

        if (CollUtil.isNotEmpty(nursingProjects)) {
            return nursingProjects;
        }
        //缓存中没有数据，从数据库查找
        List<NursingProject> nursingProjectList = memberNPMapper.selectProject(nursingProject);
        //将数据写入缓存
        redisTemplate.opsForValue().set(PROJECT_KEY, nursingProjectList);


        return nursingProjectList;
    }
}






















